
package com.tanhua.dubbo.server.api;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.mongodb.client.result.UpdateResult;
import com.tanhua.dubbo.server.pojo.Album;
import com.tanhua.dubbo.server.pojo.Publish;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

;

@Service(version = "1.0.0")
public class MessagesApiImpl implements MessagesApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 修改审核状态
     *
     * @param publishIds
     * @return
     */
    @Override
    public Boolean messagesRevocation(List<String> publishIds, String state) {
        List<ObjectId> ids = new ArrayList<>();
        for (String publishId : publishIds) {
            ids.add(new ObjectId(publishId));
        }

        Query query = Query.query(Criteria.where("id").in(ids));
        Update update = Update.update("state", state);
        UpdateResult updateResult = this.mongoTemplate.updateMulti(query, update, Publish.class);
        return updateResult.getModifiedCount() > 0;
    }

    /**
     *
     * 修改置顶
     *
     * @param publishId
     * @param topState
     * @return
     */
    @Override
    public Boolean messagesUpdateTop(String publishId, Integer topState) {
        Query query = Query.query(Criteria.where("id").is(publishId));
        Update update = Update.update("topState", topState);
        UpdateResult updateResult = this.mongoTemplate.updateMulti(query, update, Publish.class);
        return updateResult.getModifiedCount() > 0;
    }

    /**
     * 查询相册
     *
     * @param userId
     * @return
     */
    @Override
    public Set<ObjectId> queryAlbumList(Long userId) {
        //查询指定用户的相册表
        List<Album> albumList = this.mongoTemplate.findAll(Album.class, "quanzi_album_" + userId);
        if (CollectionUtils.isEmpty(albumList)) {
            return null;
        }
        //查询相册对应的动态信息
        return albumList.stream().map(Album::getPublishId).collect(Collectors.toSet());
    }


    @Override
    public List<Publish> queryPublishList(Long userId, Long ed, Long sd) {
        Criteria criteria1 = null;
        Criteria criteria2 = null;
        Criteria criteria3 = null;

        List<Criteria> criteriaList = new ArrayList<>();
        if (ed != null && sd != null && ed != -1 && sd != -1) {
            criteria1 = Criteria.where("created").gte(sd).lte(ed);
            criteriaList.add(criteria1);
        }

        // id模糊查询
        if (userId != null) {
            /*Pattern pattern = Pattern.compile( "^.*\""+userId+"\".*$" , Pattern.CASE_INSENSITIVE);
            criteria3 = Criteria.where("locationName").regex(pattern);
            criteriaList.add(criteria3);*/
            Set<ObjectId> ids = this.queryAlbumList(userId);
            criteria2 = Criteria.where("id").in(ids);
            criteriaList.add(criteria2);
        }
        if (CollectionUtils.isEmpty(criteriaList)) {
            return this.mongoTemplate.findAll(Publish.class);
        }
        Criteria[] arr = criteriaList.toArray(new Criteria[criteriaList.size()]);
        Criteria criteria = new Criteria().andOperator(arr);
        Query query = Query.query(criteria);
        return this.mongoTemplate.find(query, Publish.class);
    }

    @Override
    public List<Publish> getPublishAll() {
        return this.mongoTemplate.findAll(Publish.class);
    }
}
